<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名手机" v-model="form.username"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input placeholder="密码" type="password" v-model=" form.password"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input placeholder="确认密码" type="password" v-model="form.checkPassword"></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("你是不是没再次输入确认密码啊？"));
      } else if (value !== this.form.password) {
        callback(new Error("你是傻吗？输错啦"));
      } else {
        callback();
      }
    };
    return {
      //确认密码

      // 表单数据
      form: {
        username: "", // 登录用户名/手机
        password: "", // 登录密码
        checkPassword: "", // 确认密码
        nickname: "", // 昵称
        captcha: "" // 手机验证码
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入您的用户名或者手机号码",
            trgger: "blur"
          },
          { min: 6, max: 11, message: "长度在6-11位", trigger: "change" }
        ],
        password: [
          {
            required: true,
            message: "请输入您新密码",
            trgger: "blur"
          },
          { min: 3, max: 6, message: "长度在3-6位", trigger: "change" }
        ],

        checkPassword: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ],

        nickname: [
          {
            required: true,
            message: "请输入您的呢称",
            trgger: "blur"
          },
          { min: 1, max: 11, message: "长度在1-11位", trigger: "change" }
        ],
        captcha: [
          {
            required: true,
            message: "请输入您收到的验证码",
            trgger: "blur"
          },
          { min: 6, max: 6, message: "长度在6位", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      if (!this.form.username) {
        this.$confirm("手机号码不能为空", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }

      if (this.form.username.length !== 11) {
        this.$confirm("手机号码格式错误", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }

      this.$axios({
        url: `captchas`,
        method: "POST",
        data: {
          tel: this.form.username
        }
      }).then(res => {
        const { code } = res.data;
        this.$confirm(`模拟手机验证码为：${code}`, "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
      });
    },

    // 注册
    handleRegSubmit() {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          const { checkPassword, ...props } = this.form;
          this.$axios({
            url: "/accounts/register",
            method: "post",
            data: props
          }).then(res => {
            console.log(res.data);
            this.$notify({
              title: "恭喜您注册成功",
              message: "成为了会员,正在跳转到登录页",
              type: "success"
            });
            setTimeout(() => {
              this.$emit("tologin");
              this.form = {
                username: "",
                password: "",
                checkPassword: "",
                nickname: "",
                captcha: ""
              };
            }, 1000);
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>